<template>
  <div class="login">
    <el-card class="car">
      <div class="top">
        <img class="t1" src="@/assets/picture/login-Nv.png" alt="">
        <span class="t2">黑马面经</span>
        <div class="box" />
        <span class="t3">用户登录</span>
      </div>
      <el-form ref="form" :model="form" :rules="rules" class="form">
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            prefix-icon="el-icon-user"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            class="cen"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item prop="code">
          <div class="inp">
            <el-input
              v-model="form.code"
              class="rig"
              prefix-icon="el-icon-key"
              placeholder="请输入验证码"
            />
            <img
              ref="addimg"
              class="img"
              src="http://119.91.150.211:8080/heimamm/public/captcha?type=login"
              alt=""
              @click="sjimg"
            >
          </div>
        </el-form-item>

        <!-- `checked` 为 true 或 false -->
        <el-form-item>
          <el-checkbox
            v-model="checked"
          >我已阅读并同意 <span style="color: #409eff">用户协议</span> 和
            <span style="color: #409eff">隐私条款</span>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <div>
            <el-button
              type="primary"
              class="bot1"
              @click="submit"
            >登录</el-button>
          </div>
          <el-button
            class="bot2"
            type="primary"
            @click="register"
          >注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 显示注册页 -->
    <Register ref="register" />
  </div>
</template>

<script>
import { Login } from '@/api/login.js'
import Register from './components/register.vue'
export default {
  components: {
    Register
  },
  data() {
    return {
      checked: true,
      form: {
        phone: '18520409113', //	是	string	手机号
        password: '123456', //	是	string	密码
        code: '' //	是	string	验证码
      },
      rules: {
        phone: [
          { required: true, messgae: '请输入手机号', trigger: 'change' },
          {
            pattern:
              /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
            message: '请输入正确手机号码',
            trigger: 'change'
          }
        ],
        password: [
          { required: true, messgae: '请输入密码', trigger: 'change' },
          { min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change' }
        ],
        code: [{ required: true, messgae: '请输入验证码', trigger: 'change' }]
      }
    }
  },
  methods: {
    sjimg() {
      this.$refs.addimg.src =
        'http://119.91.150.211:8080/heimamm/public/captcha?type=login&dfhdfh=new Date()'
    },
    submit() {
      this.$refs.form.validate(async result => {
        if (result) {
          const res = await Login(this.form)
          // 存储token
          this.$store.dispatch('user/setToken', res.data.token)
          // 提示
          this.$message({
            message: '登录成功',
            type: 'success',
            duration: 1000
          })
          // 跳转
          this.$router.push('/')

          console.log(res)
        }
      })
    },
    // 点击跳转注册
    register() {
      this.$refs.register.isShow = true
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  height: 100%;
  // display: flex;
  // align-items: center;
  // justify-content: space-between;
  background: url('~@/assets/picture/login_banner_ele.c304439e.png') no-repeat
      800px 200px,
    linear-gradient(#1493fa 28%, #01c6fa 96%);
  position: relative;
  .car {
    width: 500px;
    height: 500px;
    position: absolute;
    top: 80px;
    left: 120px;
    .top {
      display: flex;
      align-items: center;
      margin: 20px;
      .t1 {
        width: 20px;
      }
      .t2 {
        font-size: 24px;
        margin: 0px 13px 0px 13px;
      }
      .box {
        height: 28px;
        border: 1px solid #c7c7c7;
        margin-right: 13px;
      }
      .t3 {
        font-size: 24px;
      }
    }
  }
  .form {
    height: 400px;
    width: 400px;
    margin-left: 20px;
    .inp {
      display: flex;
      align-items: center;
      // justify-content: center;
      .rig {
        width: 250px;
      }
      .img {
        width: 150px;
      }
    }

    .bot1 {
      height: 100%;
      width: 100%;
      margin-bottom: 25px;
    }
    .bot2 {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
